package org.tom.user.util;

import org.tom.basic.util.MD5Utils;
import org.tom.basic.util.StrUtils;
import org.tom.user.domain.Logininfo;
import org.tom.user.domain.User;
import org.tom.user.domain.WxUser;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

public class SaveUserAndLogininfo {
    public static Logininfo user2Logininfo(User user){
        Logininfo logininfo = new Logininfo();
        logininfo.setUsername(user.getPhone());
        logininfo.setPhone(user.getPhone());

        String salt = StrUtils.getComplexRandomString(32); // 随机生成32位的字符串，作为盐值
        logininfo.setSalt(salt); // 填入字段

        logininfo.setPassword(MD5Utils.encrypByMd5(salt+user.getPassword())); // 盐值加上输入的密码得到的字符串使用md5加密，然后存入数据库中的password字段

        logininfo.setType(1); // 1普通用户 ，0管理用户
        logininfo.setDisable(1); // 1启用  0禁用
        logininfo.setEmail(user.getEmail());

        return logininfo;
    }

    public static User Logininfo2User(Logininfo logininfo){
        User user = new User();
        user.setUsername(logininfo.getUsername());
        user.setEmail(logininfo.getEmail());
        user.setPassword(logininfo.getPassword());
        user.setSalt(logininfo.getSalt());
        user.setPhone(logininfo.getPhone());
        user.setState(1);
        user.setCreatetime(new Date());
        user.setLogininfo(logininfo);
        return user;

    }

    public static User wxUser2User(String phone, String headImg){
        User user = new User();
        user.setPhone(phone);
        user.setUsername(phone);
        user.setState(1);
        user.setCreatetime(new Date());
        String salt = StrUtils.getComplexRandomString(32);
        String password = MD5Utils.encrypByMd5(salt + StrUtils.getComplexRandomString(6));
        user.setSalt(salt);
        user.setPassword(password);
        user.setHeadImg(headImg);
        return user;

    }
}
